var Signup = {
	lgn: function() {
		var unm = "";
		var pwd = "";
		var type= $.query.get('type');
		if(type ==1){
			var user= GetLS("regUser");
			unm = user.email;
			pwd = user.password;
			ClearLS('reguser');
		}else {
			unm = $("#unm").val();
			pwd = $("#pwd").val();
		}
		if (!checkNull("账号",unm))
			return;
		if (!checkNull("密码",pwd))
			return;
		// if (!checkEmail(unm))
		// 	return;
		pwd = ""+CryptoJS.AES.encrypt(pwd, "gkonlearning")+"";
		$ajax({
			url:"/users/lgn",
			data:{email:unm, pwd:pwd},
			success: function(res) {
				if(res.result[0].status){
					if(res.result[0].status == 11) {
						return alert("您的账号未激活！");
					}
				}
				$(".theme-popover-mask").hide();
				$(".theme-popover").slideUp(200);
				if ($("#remeber").prop("checked") == true){
					var saveUser = new Object();
					saveUser.email = unm;
					saveUser.password = $("#pwd").val();
					SetLS("saveUser",saveUser);
				} else{
					SetLS("saveUser",null);
				}

				SetLS("user", res.result[0]);
				var CurrentUser = res.result[0];
				Signup.showLists(res.result[0].photo, res.result[0].name);
				if( res.result[0].utype != UTYPE.STUDENT && res.result[0].utype != UTYPE.TUTOR){
					$("#indexHome").hide();
					window.location.href = "/admin.html";
				}
				if(res.result[0].utype == UTYPE.TUTOR)
					window.location.href = "/tutorAdmin.html";
				// window.location.href = "/admin.html";
				// $("#loginContent,#loginC").remove();
				var html =	'	<div class="login-user" id="mocont">' + 
							'		<p class="ellipsis">' + res.result[0].name +  '</p>' + 
							'		<span><img style="position:absolute;bottom:1.5px;right:4.5px;" src="img/icon_login.png"></span>' + 
							'		<!-- 隐藏个人中心 -->' + 
							'		<div class="top_person shadow_all none" style="display: none;">' + 
							'			<p id="personal" class="home_a">个人中心</p>' + 
							'			<p id="perfect" class="home_b">完善资料</p>' + 
							'			<p id="modipass" class="home_c">修改密码</p>' + 
							'			<p id="exit" class="home_d">退出</p>' + 
							'		</div>' + 
							' 		<!-- end -->' + 
							'	</div>' + 
							'	<div class="login-text" id="tou">' + 
							'	    <img src="img/1.jpg">' + 
							'	</div>';		
				$("#mainName").html(html);

				// 首页登录 add

				var courName = "";
				var courId = "";
				var htmlA = "";

				console.log(res);
				var hrefID = "studyT"; 
				if(res.result[0].features.lastCourse != undefined){
					courName = res.result[0].features.lastCourse.name;
					courId = res.result[0].features.lastCourse.id;
					pro = CurrentUser.features.lastCourse.proAttr;
					// add  判断课程有效期 0 跳转详细页面
					for(var g=0;g<res.result[0].course.length;g++){
						if(res.result[0].course[g].validity === 0){
							if(res.result[0].course[g].name === courName){
								hrefID = "studyC"
							}
						}
					}
					// end
					
					htmlA =	'<p class="fs16 ellipsis cf">'+courName+'</p>'+
							'<p class="h_40"></p>'+
							'<p class="btnRed fs16 btnHover" id="'+hrefID+'" data-id="'+courId+'" data-pro="'+ pro +'">继续学习</p>';
							
				}else{
					htmlA = '<p class="marB20" align="center"><img src="img/loginN.png"></p>'+
							'<p class="fs16 ellipsis" align="center" style="color:#e6e6e6">您还没有任何学习记录</p>';
				}
				var htmla =	'<div class=" loginAfter">'+
							'	<div class="logTop">'+
							'		<p class="fl" id="touA"><img src="img/teacher.jpg"></p>'+
							'		<div class="fr personH"> '+
							'			<p class="fs20 ellipsis">' + res.result[0].name + '<span class="fr fs14" style="line-height:30px;"><a href="javascript:void(0)" id="exit">退出</a></span></p>'+
							'			<p style="margin-top:16px"><a href="javascript:void(0)" id="personal">个人中心</a><i style="margin:0 10px;">|</i><a id="perfect" href="javascript:void(0)">修改资料</a></p>'+
							'		</div>'+
							'	</div>'+
							'	<div class="logBom">'+
							'		<p class="cf fs14 marB20">上次学习记录：</p>'+
							'		<div style="padding:0 20px">'+
							htmlA
							'		</div>'+
							'	</div>'+
							'</div>';		
				$("#indexHome").html(htmla);
				// add
				$("#studyT").click(function(){
					var cid = $(this).data("id");
	            	var couStatus = true;
	            	for(var i=0;i<res.result[0].course.length;i++){
	            		if(cid == res.result[0].course[i].id){
	            			couStatus = res.result[0].course[i].status;
	            		}
	            	}
	            	if(couStatus){
	            		if(cid){
							// if($(this).data('pro') == 1 || $(this).data('pro') == '1')
							// 	window.location.href ="studyPlan.html?stype=course&id="+$(this).data("id")+'&pro='+$(this).data('pro');
							// else
								window.location.href ="a_s_courseLearning.html?stype=course&id="+$(this).data("id")+'&pro='+$(this).data('pro');
						}else
							alert("没有课程！");
					}else
							alert("您的这门课程已锁定，请学习其他课程或联系相关人员！！！");
					
				});
				// add 超过有效期跳转 2016年4月8日14:56:52
				$("#studyC").click(function(){
					window.location.href ="a_indexDetailCourseFirst.html?stype=course&id="+$(this).data("id");
				});
				// end
				var src = "img/tou2.jpg";
				if (res.result[0].photo){
					src = Core.id2url(res.result[0].photo);
				}
				$("#touA").html('<img src="' + src + '" style="">'); //add
				$("#tou").html('<img src="' + src + '" style="height:30px;width:30px;border-radius: 50%;right:-0.5em">');
				$("#exit").click(function(){
					if(CurrentUser && CurrentUser.utype != 1000){
						$ajax({
				            url: "/users/logout",
				            data:{email: CurrentUser.email},
				            success: function(res) {
				                ClearLS("user");
					            ClearLS(CurrentUser);
					            SetLS('CurrentUser','');
				                window.location.href = "/index.html?ran=" + Math.random();
				            }
				        });
					}else
						window.location.href = "/index.html";
				});
				
				// 弹出个人中心
				cour_n.homeTop(".login-user",".top_person");

				// $("#ssoUi").mouseover(function(){
				// 	$("#mocont").css("display","block");
				// });
				// $("#mocont").mouseout(function(){
				// 	$("#mocont").css("display","none");
				// });
				// $("#mocont div").mouseover(function(){
				// 	$(this).css("background-color","#cccccc");
				// });
				// $("#mocont div").mouseout(function(){
				// 	$(this).css("background-color","white");
				// });
				$("#perfect").click(function(){
					window.location.href = "a_modipassword.html#pert";
				});
				$("#modipass").click(function(){
					window.location.href = "a_modipassword.html#modiP";
				});
				$("#personal").click(IndexStudent.jump);

				var href = window.location.href.replace(window.location.search, "");
			    href = href.substring(href.lastIndexOf("/")+1);
			    href = href.split("#")[0];
			    if (href == "a_indexDetailCourseFirst.html" || href == "a_indexDetailCertificate.html") {
			        window.location.href = window.location.href + "&goto=1";
			    }
			    if (href == "w_marketing.html") {
			    	window.location.href = window.location.href;
			    	window.location.reload();
			    }
			   	
			}
		})

	},
	showLists: function(photo, name){
		$("#loginContent,#loginC").remove();
		// var html = 	'<div id="ssoUi" style="background-color: white;float:left;margin-right:5px;padding:0.5em;padding-left:20px;position:relative;cursor:pointer;white-space: nowrap;width:114px;right:-0.5em;">' + name + 
		// 			'	<div id="mocont" class="ub-img3" style="background-image:url(img/permod.png);display:none;cursor: pointer;position: absolute;top:135%;width: 9em;font-size: 1.2em;z-index: 9;padding-top:0.5em;left:-1em;">' + 
		// 			'		<a id="personal" style="opacity:0.9;text-decoration: none;"><div style="padding:0.5em 1.5em;opacity:0.9;color: black; text-align: center;">个人中心</div></a>' + 
		// 			'		<a id="perfect" style="opacity:0.9;text-decoration: none;"><div style="padding:0.5em 1.5em;opacity:0.9;color: black; text-align: center;">完善资料</div></a>' + 
		// 			'		<a id="modipass" style="opacity:0.9;text-decoration: none;"><div style="padding:0.5em 1.5em;opacity:0.9;color: black; text-align: center;">修改密码</div></a>' + 
		// 			'		<a id="exit" style="opacity:0.9;text-decoration: none;"><div style="padding:0.5em 1.5em;opacity:0.9;color: black; text-align: center;">退出</div></a>' + 
		// 			'	</div>' + 
		// 			'	<img style="position:absolute;bottom:1.5px;right:1.5px;" src="img/icon_login.png">' + 
		// 			'</div>' ;
		var html =	'	<div class="login-user" id="mocont">' + 
					'		<p class="ellipsis">' + name + '</p>' + 
					'		<span><img style="position:absolute;bottom:1.5px;right:4.5px;" src="img/icon_login.png"></span>' + 
					'		<!-- 隐藏个人中心 -->' + 
					'		<div class="top_person shadow_all none" style="display: none;">' + 
					'			<p id="personal" class="home_a">个人中心</p>' + 
					'			<p id="perfect" class="home_b">完善资料</p>' + 
					'			<p id="modipass" class="home_c">修改密码</p>' + 
					'			<p id="exit" class="home_d">退出</p>' + 
					'		</div>' + 
					' 		<!-- end -->' + 
					'	</div>' + 
					'	<div class="login-text">' + 
					'	    <img src="upload/'+photo+'">' + 
					'	</div>';	
		$("#mainName").html(html);
		var src = "img/tou2.jpg";
		if (photo)
			src = Core.id2url(photo);
		$("#tou").html('<img src="' + src + '" style="height:30px;width:30px;border-radius: 50%;right:-0.5em;">');
		$("#exit").click(function(){
			if(CurrentUser && CurrentUser.utype != 1000){
				$ajax({
		            url: "/users/logout",
		            data:{email: CurrentUser.email},
		            success: function(res) {
			            ClearLS('user');
			            ClearLS(CurrentUser);
		                window.location.href = "/index.html";
		            }
		        });
			}else
				window.location.href = "/index.html";
		});
		// $("#ssoUi").mouseover(function(){
		// 	$("#mocont").css("display","block");
		// });
		// $("#mocont").mouseout(function(){
		// 	$("#mocont").css("display","none");
		// });
		// $("#mocont div").mouseover(function(){
		// 	$(this).css("background-color","#cccccc");
		// });
		// $("#mocont div").mouseout(function(){
		// 	$(this).css("background-color","white");
		// });

		// 弹出个人中心
		cour_n.homeTop(".login-user",".top_person");

		$("#perfect").click(function(){
			window.location.href = "a_modipassword.html#pert";
		});
		$("#modipass").click(function(){
			window.location.href = "a_modipassword.html#modiP";
		});
		$("#personal").click(IndexStudent.jump);

		var href = window.location.href.replace(window.location.search, "");
		href = href.substring(href.lastIndexOf("/")+1);
		href = href.split("#")[0];
		if (href == "a_indexDetailCourseFirst.html" || href == "a_indexDetailCertificate.html") {
			window.location.href = window.location.href + "&goto=1";
		}
	}
}